package com.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static final String DELIMITER = "-";
	public static final String TIME_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd";
	public static final String DATA_PATTERN_YYYYMMDD = "yyyyMMdd";
	public static final String TIME_PATTERN_HHMMSS = "HH:mm:ss";
	public static final int ONE_SECOND = 1000;
	public static final int ONE_MINUTE = 60 * ONE_SECOND;
	public static final int ONE_HOUR = 60 * ONE_MINUTE;
	public static final long ONE_DAY = 24 * ONE_HOUR;

	public static Date getDate() {
		Calendar cal = Calendar.getInstance();
		Date currDate = cal.getTime();

		return currDate;
	}

	public static Date parseDate(String date) {
		return parseDate(DATE_PATTERN_DEFAULT, date);
	}

	public static Date parseDateTime(String date) {
		return parseDate(TIME_PATTERN_DEFAULT, date);
	}

	public static Date parseDate(String date, String pattern) {
		if (date == null) {
			return null;
		}

		if (pattern == null) {
			pattern = TIME_PATTERN_DEFAULT;
		}

		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		Date newDate = null;

		try {
			newDate = dateFormat.parse(date);
		} catch (ParseException pe) {
			newDate = null;
		}

		return newDate;
	}

	public static String format(Date date) {
		return format(date, DATE_PATTERN_DEFAULT);
	}

	public static String formatTime(Date date) {
		return format(date, TIME_PATTERN_DEFAULT);
	}

	public static String format(Date date, String pattern) {
		if (date == null || pattern == null) {
			return null;
		}

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(pattern);

		return dateFromat.format(date);
	}

	public static String formatTime(Date date, String pattern) {
		if (date == null || pattern == null) {
			return null;
		}

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(pattern);

		return dateFromat.format(date);
	}

	public static String formatCurrentDate() {
		return format(new Date(), DATE_PATTERN_DEFAULT);
	}

	public static String formatCurrentTime() {
		return format(new Date(), TIME_PATTERN_DEFAULT);
	}

	public static String formatCurrentYear() {
		return format(new Date(), "yy");
	}

	public static String formatCurrentMonth() {
		return format(new Date(), "MM");
	}

	public static String formatCurrentDay() {
		return format(new Date(), "dd");
	}

	public static void main(String[] args) {
		// System.out.println(DateUtils.getDate());
		// System.out.println(DateUtils.parseDate("2008-11-03"));
		// System.out.println(DateUtils.parseDateTime("2008-11-03 10:02:30"));
		// System.out.println(DateUtils.parseDate("yyyy-MM-dd", "2008-05-12"));

		System.out.println(DateUtils.formatTime(new Date()));
		System.out.println(DateUtils.format(new Date()));
		System.out.println(DateUtils.format(new Date(), "yy-MM-dd"));
		System.out.println(DateUtils.format(new Date(), "yyyy"));
		System.out.println(DateUtils.formatTime(new Date(), "mm:ss"));
	}
}
